package logic

import (
	"context"
	"errors"
	"shop_srv/models"

	"shop_srv/internal/svc"
	"shop_srv/shop"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddActivityLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddActivityLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddActivityLogic {
	return &AddActivityLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddActivityLogic) AddActivity(in *shop.AddActivityRequest) (*shop.AddActivityResponse, error) {
	// todo: add your logic here and delete this line

	var err error
	var g models.Goods
	var a models.Activity

	if err = g.GetGoodsById(in.GoodsId); err != nil {
		logx.Error("获取商品信息失败")
		return nil, errors.New("获取商品信息失败")
	}
	if g.ID == 0 {
		logx.Error("该商品不存在")
		return nil, errors.New("该商品不存在")
	}

	a.GoodsID = int(in.GoodsId)
	a.ActivityPrice = float64(in.Price)
	a.Progress = in.Progress
	a.Countdown = in.Countdown
	if err = a.AddActivity(); err != nil {
		logx.Error("活动创建失败")
		return nil, errors.New("活动创建失败")
	}

	return &shop.AddActivityResponse{
		ActivityId: int64(a.ID),
	}, nil
}
